﻿using Newtonsoft.Json;
using System;

namespace CMS.Infos
{
    public class Term : ICloneable, IAssignable
    {
        /// <summary>
        /// 学期的ID
        /// </summary>
        [JsonProperty(PropertyName = "id")]
        public int ID { get; set; } = 0;

        /// <summary>
        /// 学期的名称
        /// </summary>
        [JsonProperty(PropertyName = "name")]
        public string Name { get; set; }

        /// <summary>
        /// 学期的开始日期
        /// </summary>
        [JsonProperty(PropertyName = "start_date", NullValueHandling = NullValueHandling.Ignore)]
        public string StartDate
        {
            get => StartDateRaw.ToShortDateString();
            set => StartDateRaw = DateTime.Parse(value);
        }

        /// <summary>
        /// 学期的开始日期(原始数据,用于本地的读写)
        /// </summary>
        [JsonIgnore]
        public DateTime StartDateRaw { get; set; } = DateTime.Now.Date;
        public object Assign(object src)
        {
            if (src is Term src2)
            {
                ID = src2.ID;
                Name = src2.Name;
                StartDateRaw = src2.StartDateRaw;
            }
            else
            {
                throw new ArithmeticException();
            }
            return this;
        }

        public object Clone()
        {
            return new Term() { ID = ID, Name = Name, StartDateRaw = StartDateRaw };
        }

        public override int GetHashCode()
        {
            return ID ^ (Name?.GetHashCode() ?? 0) ^ StartDateRaw.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            if (obj is Term term)
            {
                return term.ID == ID && term.Name == Name && term.StartDateRaw == StartDateRaw;
            }
            else
            {
                return false;
            }
        }

        public override string ToString()
        {
            return Name;
        }

    }
}
